package com.zige.zige.activity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zige.zige.R;
import com.zige.zige.dialog.InterceptionImageDialog;
import com.zige.zige.view.CircleImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Calendar;

/**
 * Created by Fei on 2016/3/9.
 *
 * 完善资料界面
 */
public class CompleteInfomation extends BaseActivity implements View.OnClickListener,InterceptionImageDialog.HeadIconPathListener{

    private CircleImageView baby_head_img;

    private EditText baby_nickname;
    private RadioGroup rg_sex;
    private RadioButton rg_sex_boy, rg_sex_girl;
    private TextView tv_baby_birthday,tv_baby_sex;
    private Button  btn_next;

    private String babySex, babyBirthday;

    private String headImagePath, headImageName, headImageUrl;

    private String uId,userAcount,userPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_completeinfomation);
        uId = getIntent().getStringExtra("uerId");
        userAcount =getIntent().getStringExtra("userName");
        userPassword = getIntent().getStringExtra("userPassword");
        initView();
        setImageCacheFile();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(headImagePath !=null){
            Bitmap bitmap = getLoacalBitmap(headImagePath);
            baby_head_img.setImageBitmap(bitmap);
            headImageName = getFileName(headImagePath);
            headImageUrl = getQniuImageUrl(headImageName);
        }

    }

    private void initView() {

        baby_head_img = serchViewById(R.id.baby_head_img);
        baby_head_img.setOnClickListener(this);

        baby_nickname = serchViewById(R.id.baby_nickname);
        tv_baby_sex = serchViewById(R.id.tv_baby_sex);
        tv_baby_sex.setOnClickListener(this);
        rg_sex = serchViewById(R.id.rg_sex);
        rg_sex_boy = serchViewById(R.id.rg_sex_boy);
        rg_sex_girl = serchViewById(R.id.rg_sex_girl);
        rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioButtonId = group.getCheckedRadioButtonId();
                RadioButton rb = (RadioButton) CompleteInfomation.this.findViewById(radioButtonId);
                babySex = String.valueOf(rb.getText());
            }
        });

        tv_baby_birthday = serchViewById(R.id.tv_baby_birthday);
        tv_baby_birthday.setOnClickListener(this);
        btn_next = serchViewById(R.id.btn_next);
        btn_next.setOnClickListener(this);

    }

    String baby_sex;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.baby_head_img:
                InterceptionImageDialog dialog = new InterceptionImageDialog();
                dialog.show(getFragmentManager(), "dialog");

                break;
            case R.id.tv_baby_sex:
                showRemoveDialog();
                break;
            case R.id.tv_baby_birthday:
                Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(CompleteInfomation.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        babyBirthday = String.valueOf(year) + "-" + String.valueOf(monthOfYear + 1) + "-" + String.valueOf(dayOfMonth);
                        tv_baby_birthday.setTextColor(getResources().getColor(R.color.m_btn_text_select));
                        tv_baby_birthday.setTextSize(16.0f);
                        tv_baby_birthday.setText(babyBirthday);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

                break;
            case R.id.btn_next:
                if(TextUtils.isEmpty(baby_nickname.getText().toString())){
                    showErrorDialog("宝贝昵称还没有填写");
                    return;
                }
                if(TextUtils.isEmpty(tv_baby_sex.getText().toString())){
                    showErrorDialog("宝贝性别还没有填写");
                    return;
                }
                if(TextUtils.isEmpty(babyBirthday)){
                    showErrorDialog("宝贝生日还没有填写");
                    return;
                }

                if(tv_baby_sex.getText().toString().equals("男")){
                    baby_sex= "M";
                }else if(tv_baby_sex.getText().toString().equals("女")){
                    baby_sex= "F";
                }
                Intent intent = new Intent();
                intent.putExtra("uerId", uId);
                intent.putExtra("userName", userAcount);
                intent.putExtra("userPassword", userPassword);
                intent.putExtra("headImageName",headImageName);
                intent.putExtra("headImagePath",headImagePath);
                intent.putExtra("headImageUrl",headImageUrl);
                intent.putExtra("babyName",baby_nickname.getText().toString());
                intent.putExtra("babySex",baby_sex);
                intent.putExtra("babybirthday",babyBirthday);
                intent.setClass(CompleteInfomation.this, UserSelectVideoTagsActivity.class);
                startActivity(intent);
                break;
        }
    }


    @Override
    public void OnHeadIconPath(String path) {
        headImagePath = path;


    }
    /**
     * 加载本地图片
     *
     * @param url
     * @return
     */
    public static Bitmap getLoacalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }



    private void  setImageCacheFile(){
        File tmpDir = new File(Environment.getExternalStorageDirectory() + "/Littlezebra");
        if (!tmpDir.exists()) {
            tmpDir.mkdir();
        }

    }
    /**
     * 根据路径得到文件得到文件名
     *
     * @param pathandname
     * @return
     */
    public String getFileName(String pathandname) {

        int start = pathandname.lastIndexOf("/");
        int end = pathandname.lastIndexOf(".");
        if (start != -1 && end != -1) {
            return pathandname.substring(start + 1, end);
        } else {
            return null;
        }
    }
    /**
     * 拼接七牛图片地址
     *
     * @param name
     * @return
     */
    private String getQniuImageUrl(String name) {
        return "http://" + "7xkpsq.com2.z0.glb.qiniucdn.com" + "/" + name;
    }


    private void showRemoveDialog() {
        View view = View.inflate(this, R.layout.dialog_select_sex, null);
        final Dialog alertDialog = new Dialog(this, R.style.theme_dialog);
        alertDialog.setContentView(view);
        view.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                tv_baby_sex.setTextColor(getResources().getColor(R.color.m_btn_text_select));
                tv_baby_sex.setTextSize(16.0f);
                tv_baby_sex.setText("男");

            }
        });
        view.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                tv_baby_sex.setTextColor(getResources().getColor(R.color.m_btn_text_select));
                tv_baby_sex.setTextSize(16.0f);
                tv_baby_sex.setText("女");
            }
        });
        alertDialog.show();
    }


}
